package com.cjm.one.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.model.base.BaseDeleteMoreVo;
import com.cjm.one.model.system.dto.SysRoleDto;
import com.cjm.one.model.system.entity.SysRoleRes;
import com.cjm.one.model.system.entity.SysUserPost;
import com.cjm.one.model.system.vo.RoleDetailVO;
import com.cjm.one.system.mapper.SysRoleMapper;
import com.cjm.one.system.mapper.SysRoleResMapper;
import com.cjm.one.system.mapper.SysUserPostMapper;
import com.cjm.one.system.service.SysRoleService;
import com.cjm.one.model.system.entity.SysRole;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 29289
* @description 针对表【sys_role(角色表)】的数据库操作Service实现
* @createDate 2024-09-05 16:21:14
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserPostMapper sysUserPostMapper;
    @Resource
    private SysRoleResMapper sysRoleResMapper;

    @Override
    public List<SysRole> getAllRoleList() {

        return sysRoleMapper.selectAllRole();
    }

    @Override
    public Page<SysRole> getRolePage(Page<SysRole> page, SysRoleDto sysRoleDto) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (sysRoleDto != null) {
            if (sysRoleDto.getRoleName() != null) {
                wrapper.like(SysRole::getRoleName, sysRoleDto.getRoleName());
            }
            if (sysRoleDto.getRoleKey() != null) {
                wrapper.like(SysRole::getRoleKey, sysRoleDto.getRoleKey());
            }
            if (sysRoleDto.getRoleStatus() != null) {
                wrapper.eq(SysRole::getRoleStatus, sysRoleDto.getRoleStatus());
            }
            if (sysRoleDto.getRoleSort() != null) {
                wrapper.orderByAsc(SysRole::getRoleSort);
            }

        }
        return sysRoleMapper.selectPage(page, wrapper);
    }

    @Override
    public Boolean updateRole(RoleDetailVO sysRole) {
        LambdaQueryWrapper<SysRoleRes> sysRoleResLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleResLambdaQueryWrapper.eq(SysRoleRes::getRoleId, sysRole.getId());
        int delete = sysRoleResMapper.delete(sysRoleResLambdaQueryWrapper);
        ArrayList<SysRoleRes> sysRoleRes1 = new ArrayList<>();

        sysRole.getPermissions().forEach(menuId -> {
            SysRoleRes sysRoleRes = new SysRoleRes();
            sysRoleRes.setRoleId(sysRole.getId());
            sysRoleRes.setResId(menuId);
            sysRoleRes1.add(sysRoleRes);
        });
        sysRoleResMapper.insert(sysRoleRes1);
        int i = sysRoleMapper.updateById(sysRole);
        return i==1;
    }

    @Override
    public Boolean addSysRole(RoleDetailVO sysRole) {
        int insert = sysRoleMapper.insert(sysRole);
        List<BatchResult> insert1 = null;
        if (insert == 1) {
            //添加角色和菜单关联
            ArrayList<SysRoleRes> sysRoleResArrayList = new ArrayList<>();
            sysRole.getPermissions().forEach(menuId -> {
                SysRoleRes sysRoleRes = new SysRoleRes();
                sysRoleRes.setRoleId(sysRole.getId());
                sysRoleRes.setResId(menuId);
                sysRoleResArrayList.add(sysRoleRes);
            });
            insert1= sysRoleResMapper.insert(sysRoleResArrayList);

        }


        return insert1.size() == sysRole.getPermissions().size();
    }

    @Override
    public Boolean deleteById(Long id) {
        //TODO:  事务
        // 删除用户岗位关联表数据
        LambdaQueryWrapper<SysUserPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserPost::getUserId, id);
        sysUserPostMapper.delete(wrapper);
//        LambdaQueryWrapper
        int i = sysRoleMapper.deleteById(id);
        return i== 1;
    }

    @Override
    public Boolean updateStatus(SysRole sysRole) {
        if (sysRole.getRoleStatus() !=null) {
            LambdaUpdateWrapper<SysRole> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(SysRole::getRoleStatus, sysRole.getRoleStatus());
            wrapper.eq(SysRole::getId, sysRole.getId());
            int update = sysRoleMapper.update(null, wrapper);
            return update ==1;
        }

        return false;
    }

    @Override
    public BaseDeleteMoreVo removeBySysRoleIds(List<Long> ids) {
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUserPost::getUserId, ids);
        SysUserPost sysUserPost = sysUserPostMapper.selectOne(queryWrapper);
        if (sysUserPost != null) {
            throw  new GlobalException(ResultCodeEnum.DELETE_ROLE_ERROR);
        }
        BaseDeleteMoreVo baseDeleteMoreVo = new BaseDeleteMoreVo();
        baseDeleteMoreVo.setTotal(ids.size());

        int i = sysRoleMapper.deleteBatchIds(ids);
        baseDeleteMoreVo.setCount(i);
        baseDeleteMoreVo.setFlag(false);
        if (i == ids.size()) {
            baseDeleteMoreVo.setFlag(true);
            return baseDeleteMoreVo;
        }
        return baseDeleteMoreVo;

    }

    @Override
    public RoleDetailVO selectRoleDetail(Long id) {
        return sysRoleMapper.selectRoleDetail(id);
    }


}




